package com.kantboot.wechat.applet.entity;


import com.alibaba.fastjson.JSON;
import com.kantboot.util.WebSentUtil;
import com.kantboot.wechat.applet.config.WechatAppletConfig;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

public class AuthGetAccessTokenEntity implements Serializable {

    public static String REQUEST_URL = "https://api.weixin.qq.com/cgi-bin/token";

    @Data
    @Accessors(chain = true)
    public static class Param {
        private String appid;
        private String secret;
        private String grant_type = "client_credential";

        public static Param createInstance(WechatAppletConfig wechatAppletConfig) {
            Param param = new Param();
            param.setAppid(wechatAppletConfig.getAppid());
            param.setSecret(wechatAppletConfig.getSecret());
            return param;
        }

    }

    @Data
    @Accessors(chain = true)
    public static class Result {
        private String accessToken;
        private String expiresIn;
        private Integer errcode;
        private String errmsg;

        private WechatAppletConfig wechatAppletConfig;

        public Result analysis(Param param){
            param.setAppid(wechatAppletConfig.getAppid())
                    .setSecret(wechatAppletConfig.getSecret());
            String result= WebSentUtil.sendGet(AuthCode2SessionEntity.REQUEST_URL,param);
            return JSON.parseObject(result, Result.class);
        }
    }

}
